package cn.wolfcode.web.controller;

import cn.wolfcode.domain.City;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Province;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@Controller
public class JsonController {

    //JSON
    @RequestMapping("/cities")
    @ResponseBody
    public List<City> getCity(Long pid) {
        //模拟一下
        List<City> cities = City.getCityByProvinceId(pid);
        return cities;
    }



    //JSON
    @RequestMapping("/provinces")
    @ResponseBody
    public List<Province>  getProvince() {
        //模拟一下
        List<Province> provinces = Province.getAllProvince();
        return provinces;
    }




    //若登录成功{"success":true, "msg":"登陆成功"}

    @RequestMapping("/loginJson")
    @ResponseBody
    public JsonResult checkUsername(String username, String password) {
        JsonResult jsonResult = new JsonResult();
        if ("lony".equals(username) && "123".equals(password)) {
            jsonResult.setSuccess(true);
            jsonResult.setMsg("登录成功");
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMsg("登录失败");
        }
        return jsonResult;
    }


    @RequestMapping("/checkUsername")
    @ResponseBody
    public JsonResult checkUsername(String username) {
        //数据库查询
        JsonResult jsonResult = new JsonResult();
        if ("lony".equals(username)) {
            jsonResult.setSuccess(false);
            jsonResult.setMsg("用户名已注册");
        } else {
            jsonResult.setSuccess(true);
            jsonResult.setMsg("恭喜你可以入坑");
        }

        return jsonResult;
    }

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/depts")
    @ResponseBody
    public List<Department> getDepts() {
        return departmentService.listAll();
    }

    @RequestMapping("/getTime")
    @ResponseBody
    public JsonResult getTime() {
        Date now = new Date();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(true);
        jsonResult.setMsg(now.toLocaleString());
        return jsonResult;
    }
}
